package com.google.code.tretris;

import java.util.ArrayList;
import java.util.Collection;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.google.code.tretris.network.NetworkManager;
import com.google.code.tretris.network.connection.TretrisXMPPConnection;
import com.google.code.tretris.network.exceptions.ConnectionFailedException;
import com.google.code.tretris.network.exceptions.NotConnectedException;
import com.google.code.tretris.network.peers.NetworkClient;
import com.google.code.tretris.server.GamePlayService;
import com.google.code.tretris.server.GamePlayer;

public class InvitePlayersActivity extends Activity {
	
	private final static String LOG_TAG = "InvitePlayersActivity"; 
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.invite_players);

		SharedPreferences settings = getSharedPreferences(SettingsDialog.PREFS_NAME, 0);
		final String email = settings.getString(SettingsDialog.USER_ID, "");
		final String password = settings.getString(SettingsDialog.PASSWORD, "");

		final EditText playersText = (EditText) findViewById(R.id.invite_players_text);
		Button inviteButton = (Button) findViewById(R.id.invite_button);

		inviteButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String[] emails = playersText.getText().toString().split(",");
				try {
					setupGameplay(email.trim(), password.trim(), emails);
					Intent intent = new Intent(getApplicationContext(), GamePlayService.class);
					Log.d(LOG_TAG, "Intenting service to start") ;
					startService(intent) ;
					
					Intent nextActivity = new Intent(getApplicationContext(), Tretris.class);
					startActivity(nextActivity);
				} catch (NoPlayersFoundException e) {
					AlertDialog.Builder builder = new AlertDialog.Builder(InvitePlayersActivity.this);
					builder.setMessage(R.string.error_noplayers) ;
					builder.setCancelable(false);
					builder.setNeutralButton(R.string.dialog_noplayers, new DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
						}
					});
					builder.show();
				}
			}
		});
	}

	private void setupGameplay(String user, String password, String[] emails)
	throws NoPlayersFoundException {

		Collection<String> players = new ArrayList<String>();
		ArrayList<GamePlayer> clients = new ArrayList<GamePlayer>();
		players.add(user);
		for (String email : emails) {
			if (!email.contains("@")) {
				email += "@gmail.com" ;
			}
			players.add(email.trim());
			Log.d(LOG_TAG, "Trying to invite player " + email);
		}
		
		NetworkManager networkManager = NetworkManager.newInstance(
				new TretrisXMPPConnection());
		try {
			networkManager.connect(user, password);
			networkManager.addPlayers(players);
			networkManager.broadcastControll();

		} catch (ConnectionFailedException e) {
			Log.e(LOG_TAG, e.getMessage(), e);
		} catch (NotConnectedException e) {
			Log.e(LOG_TAG, e.getMessage(), e);
		}
		
		// Adda bara clients som fås från networkManager. sållad efter Roster
		for (NetworkClient client : networkManager.getClients()) {
			clients.add(new GamePlayer(client.getClientID()));
		}

		if (clients.size() < 1) {
			throw new NoPlayersFoundException();
		}
		
		// Bind to TretrisServer and set up the game...
	}
}
